//
//  SinaVisitorView.swift
//  weibo
//
//  Created by guyue on 16/1/1.
//  Copyright © 2016年 guyue. All rights reserved.
//

import UIKit

//代理声明 --必须继承自NSObjectProtocol,这样才可以使用weak
protocol SinaVisitorViewDelegate: NSObjectProtocol {
    
    //回调方法
    //登录
    func didLoginMethod()
    //注册
    func didRegisterMethod()
}

class SinaVisitorView: UIView {
    
    weak var delegate: SinaVisitorViewDelegate?
    
    //构造函数,调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:--动画方法
     func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置属性
        animation.toValue = M_PI*2
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 20
        
        //当完成动画,或者视图进入后台之后,会默认移除动画,把removedOnCompletion改成false就不会移除动画了
        
        animation.removedOnCompletion = false
        
        //添加动画
        animationImageView.layer.addAnimation(animation, forKey: nil)
    
    }
    
    
    //定义一个方法,由子类调用,
    //通过设置图片为可选项,判断是否需要显示首页小房子动画,有图的话就不显示
    
    func setUpInfo(message: String,image: String?) {
        if let NewImage = image {
            
            //这里是非首页,让动画隐藏
            animationImageView.hidden = true
            
            houseImageView.image = UIImage(named: NewImage)
        
        }else{
        
        animationImageView.hidden = false
        
        }
        messageLabel.text = message
    }
    
    //MARK:  登录和注册按钮的点击事件
    func registerMethod() {
    
        printLog("注册")
        
        //OC里面判断了一下,但是这里这个可选项,相当于做了判断
        delegate?.didRegisterMethod()
    }
    
    func loginMethod() {
    
        printLog("登录")
        delegate?.didLoginMethod()
    }
    
   //懒加载背面的动画  是第一步
   //MARK:--背面的动画
    
   private lazy var animationImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
    
        img.sizeToFit()
        
        return img
    }()
    
    //添加遮盖视图
    private var maskImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    
        
    }()
    
    
    //首页的小房子
   private lazy var houseImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        //img.sizeToFit()
        
        return img
    }()
    
    //提示语
    
   private lazy var messageLabel: UILabel = {
    
        let lb = UILabel()
        
        //属性
        
        lb.text = "关注一些人，回这里看看有什么惊喜  关注一些人，回这里看看有什么惊喜"
        
        lb.textColor = UIColor.darkGrayColor()
        
        lb.textAlignment = .Center
        
        //设置行数
        lb.numberOfLines = 0
        
        return lb
    
    }()
    
    //注册按钮
    
   private lazy var registerButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
    
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
    
    //给注册按钮添加点击的方法
    button.addTarget(self, action: "registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    
   private lazy var loginButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
    //给登录按钮添加方法
    button.addTarget(self, action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    
    }()
    
}


    //进行代码分块
    extension SinaVisitorView {
        
        //MARK:--添加到视图里
        private func setupUI() {
            
            //添加背景颜色
            //backgroundColor = UIColor.whiteColor()
            
            backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
            
            
            //添加动画视图
            addSubview(animationImageView)
            
            //把遮盖视图添加到父视图里面
            addSubview(maskImageView)
            
            addSubview(houseImageView)
            
            addSubview(messageLabel)
            
            addSubview(registerButton)
            
            addSubview(loginButton)
            
            
            
            //添加约束
            
            //首页动画图片的约束
            //手写约束的时候,首先要关闭系统的autosizing
            //不去除的话约束会有问题
            animationImageView.translatesAutoresizingMaskIntoConstraints = false
            maskImageView.translatesAutoresizingMaskIntoConstraints = false
            houseImageView.translatesAutoresizingMaskIntoConstraints = false
            messageLabel.translatesAutoresizingMaskIntoConstraints = false
            registerButton.translatesAutoresizingMaskIntoConstraints = false
            loginButton.translatesAutoresizingMaskIntoConstraints = false
            
            
            
            
            //动画水平居中,重置居中
            
            addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
            
            //添加遮盖视图的约束  与父视图等宽等高,然后水平居中,垂直居中
            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: -350))
            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
            

//            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
//            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
//            
//            addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: -350))

            
            //首页小房子的约束 水平垂直居中
            addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
            
            //提示语的约束,距离顶部是animationImageView的底部 宽度224,水平居中
            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: animationImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
            addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            
            //注册按钮 宽 100 高 30 左边和messageLabel的左边对齐, 顶部和其底部对齐
            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
            addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
            
            //登录按钮 宽 100 高 30 右边和messageLabel的右边对齐,顶部和其底部对齐
            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
            addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
            
            
            
        }

    
    }
    
    
    

